﻿/*
This file is under Microsoft Public License (MS-PL)
Copyright Yohann MARTEL.  All rights reserved.
Codeplex: http://amf.codeplex.com
AMF Website: http://www.ymartel.com/aspnet-mobile-framework/
*/
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Mobile.WebControls.Field
{
    public class ComboBox : DataBoundControl
    {
        #region Properties
        /// <summary>
        /// Gets or sets a value indicating whether [data native menu].
        /// Default is true.
        /// </summary>
        /// <value><c>true</c> if [data native menu]; otherwise, <c>false</c>.</value>
        [Category("Appearance")]
        public bool DataNativeMenu { get; set; } 

        [Category("Data")]
        public string DataTextField { get; set; }

        [Category("Data")]
        public string DataValueField { get; set; }

        [Category("Data")]
        public string DataGroupByField { get; set; }

        [Category("Appearance")]
        public DataTheme Theme { get; set; }

        [Category("HTML")]
        public string FieldName { get; set; }

        [Category("Functionality")]
        public bool MultipleSelect { get; set; }

        [Category("Appearance")]
        public string Title { get; set; }

        [Category("Appearance")]
        public string LabelText { get; set; }
        #endregion

        #region Constructor
        public ComboBox()
        {
            DataNativeMenu = true;
        }
        #endregion

        #region Methods
        protected override void PerformDataBinding(System.Collections.IEnumerable datas)
        {
            Label label = new Label {Text = LabelText, UsedForComboBox = true};
            Controls.Add(label);

            SelectControl selectControl = new SelectControl();
            //if we use multiple select, we've to set DataNativeMenu to false
            if (MultipleSelect)
            {
                DataNativeMenu = false;
            }
            selectControl.MultipleSelect = MultipleSelect;
            selectControl.DataNativeMenu = DataNativeMenu;
            selectControl.FieldName = FieldName;
            Controls.Add(selectControl);
            label.ForId = selectControl.ClientID;

            
            if ((!DataNativeMenu) && (!String.IsNullOrEmpty(Title)))
            {
                SelectOption firstItem = new SelectOption();
                firstItem.Text = Title;
                selectControl.Controls.Add(firstItem);
            }

            if (datas != null)
            {
                string lastGroupby = null;
                SelectOptionGroup selectOptionGroup = null;
                foreach (object dataItem in datas)
                {
                    //get the optiongroup control
                    if (!String.IsNullOrEmpty(DataGroupByField))
                    {
                        object propGroupby = DataBinder.GetPropertyValue(dataItem, DataGroupByField);
                        if (propGroupby != null)
                        {
                            if ((lastGroupby == null) || (lastGroupby != propGroupby.ToString()))
                            {
                                lastGroupby = propGroupby.ToString();
                                selectOptionGroup = new SelectOptionGroup {Text = lastGroupby};
                                selectControl.Controls.Add(selectOptionGroup);
                            }
                        }
                    }

                    //get the databound property
                    object propTextField = DataBinder.GetPropertyValue(dataItem, DataTextField);
                    object propValueField = DataBinder.GetPropertyValue(dataItem, DataValueField);
                    SelectOption selectOption = new SelectOption();
                    if (propTextField != null)
                    {
                        selectOption.Text = propTextField.ToString();
                    }
                    if (propValueField != null)
                    {
                        selectOption.Value = propValueField.ToString();
                    }

                    //we've to check if we're in GroupBy Mode
                    if (selectOptionGroup == null) 
                    {
                        selectControl.Controls.Add(selectOption);
                    }
                    else
                    {
                        selectOptionGroup.Controls.Add(selectOption);
                    }
                }
            }
        }

        #endregion

        #region Render Methods
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            AddAttributesToRender(writer);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
        }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.RenderEndTag(); // </div>
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute(MetadataAttribute.DataRole, DataRole.Fieldcontain);
            if (Theme != DataTheme.None)
            {
                writer.AddAttribute(MetadataAttribute.DataTheme, Enum.GetName(typeof(DataTheme), Theme).ToLower());
            }
        }
        #endregion
    }
}
